﻿namespace Sidvall.WorkItemManager.Net.Services.V1
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
    public sealed partial class WebExecutionContext : global::Sidvall.Data.ServiceDataRowBase, global::Sidvall.WorkItemManager.Data.IWebExecutionContext
    {
        #region Public Members

        #region WebExecutionContextId

        private System.Nullable<int> _WebExecutionContextId;
        partial void OnBeforeWebExecutionContextIdPropertyChanged(Sidvall.Business.PropertyChangeEventData<System.Nullable<int>> eventData);
        partial void OnAfterWebExecutionContextIdPropertyChanged();
        public System.Nullable<int> WebExecutionContextId
        { 
            get
            {
                return _WebExecutionContextId;
            }
            set
            {
                if (this.BusinessSettings.EnabledBusinessEvents == Sidvall.Business.BusinessEvents.None)
                {
                    _WebExecutionContextId = value;
                }
                else
                {
                    Sidvall.Business.PropertyChangeEventData<System.Nullable<int>> eventData = new Sidvall.Business.PropertyChangeEventData<System.Nullable<int>>(value, _WebExecutionContextId);
                    OnBeforeWebExecutionContextIdPropertyChanged(eventData);
                    if (!eventData.Cancel)
                    {
                        LoadInstanceField("WebExecutionContextId", eventData.Value, true, true);
                        OnAfterWebExecutionContextIdPropertyChanged();
                    }
                }
            }
        }

        #endregion
        #region DataRowContextStorageKey

        protected override string DataRowContextStorageKey
        {
            get
            {
                return global::Sidvall.WorkItemManager.SystemContextConfig.Current.EntityFacade.WebExecutionContextManager.GetKey(this);
            }
        }

        #endregion
        #region DataRowStateIsDirty

        protected override bool DataRowStateIsDirty
        {
            get
            {
                return global::Sidvall.WorkItemManager.SystemContextConfig.Current.EntityFacade.WebExecutionContextManager.DataRowStateIsDirty(this);
            }
        }

        #endregion
        
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public WebResultCollectionItem WebResults { get; set; }
        #region LoadWebResults

        public void LoadWebResults(WebResultCollectionItem value)
        {
            LoadInstanceField("WebResults", value, true, false);
        }

        #endregion
        #region LoadInstanceField

        public void LoadInstanceField(string fieldName, object value, bool cascade, bool updateState)
        {
            global::Sidvall.WorkItemManager.SystemContextConfig.Current.EntityFacade.WebExecutionContextManager.LoadInstanceField(this, fieldName, value, cascade, false, updateState);
        }

        #endregion
        #region Copy

        public WebExecutionContext Copy()
        {
            return global::Sidvall.WorkItemManager.SystemContextConfig.Current.EntityFacade.WebExecutionContextManager.Copy(this);
        }
        public WebExecutionContext Copy(Sidvall.Data.CopySettings settings)
        {
            return global::Sidvall.WorkItemManager.SystemContextConfig.Current.EntityFacade.WebExecutionContextManager.Copy(this, settings);
        }

        #endregion
        #region Import

        public void Import(global::Sidvall.WorkItemManager.Data.IWebExecutionContext item)
        {
            global::Sidvall.WorkItemManager.SystemContextConfig.Current.EntityFacade.WebExecutionContextManager.Import(this, item);
        }
        public void Import(global::Sidvall.WorkItemManager.Data.IWebExecutionContext item, Sidvall.Data.CopySettings settings)
        {
            global::Sidvall.WorkItemManager.SystemContextConfig.Current.EntityFacade.WebExecutionContextManager.Import(this, item, settings);
        }

        #endregion
        #region ValidateAsync

        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync()
        {
            return await global::Sidvall.WorkItemManager.SystemContextConfig.Current.EntityFacade.WebExecutionContextManager.ValidateAsync(this).ConfigureAwait(false);
        }
        public async System.Threading.Tasks.Task<Sidvall.Data.BusinessRuleCollection> ValidateAsync(Sidvall.Data.ValidationSettings settings)
        {
            return await global::Sidvall.WorkItemManager.SystemContextConfig.Current.EntityFacade.WebExecutionContextManager.ValidateAsync(this, settings).ConfigureAwait(false);
        }

        #endregion
        #region Initialize

        public void Initialize()
        {
            global::Sidvall.WorkItemManager.SystemContextConfig.Current.EntityFacade.WebExecutionContextManager.Initialize(this);
        }
        public void Initialize(Sidvall.Data.InitializeSettings settings)
        {
            global::Sidvall.WorkItemManager.SystemContextConfig.Current.EntityFacade.WebExecutionContextManager.Initialize(this, settings);
        }

        #endregion

        #region IWebExecutionContext Members

        System.Nullable<int> global::Sidvall.WorkItemManager.Data.IWebExecutionContext.WebExecutionContextId
        {
            get
            {
                return this._WebExecutionContextId;
            }
            set
            {
                this._WebExecutionContextId = value;
            }
        }
        System.Collections.Generic.IEnumerable<global::Sidvall.WorkItemManager.Data.IWebResult> global::Sidvall.WorkItemManager.Data.IWebExecutionContext.WebResults
        {
            get
            {
                if (this.WebResults != null)
                    return this.WebResults.Items;
                return null;
            }
        }
        void global::Sidvall.WorkItemManager.Data.IWebExecutionContext.SetWebResults(Sidvall.Data.IDataRowProvider<global::Sidvall.WorkItemManager.Data.IWebResult> items)
        {
            this.WebResults = (WebResultCollectionItem)items;
        }

        #endregion

        #endregion
        #region Constructors

        public WebExecutionContext()
        {
        }
        public WebExecutionContext(bool initializeDefaultValues)
            : this(initializeDefaultValues, null, Sidvall.Business.BusinessEvents.None)
        {
        }
        public WebExecutionContext(bool initializeDefaultValues, Sidvall.Data.DataRowState? dataRowState, Sidvall.Business.BusinessEvents enabledBusinessEvents)
            : base(dataRowState, enabledBusinessEvents)
        {
            if (initializeDefaultValues)
                global::Sidvall.WorkItemManager.SystemContextConfig.Current.EntityFacade.WebExecutionContextManager.Initialize(this);
        }

        #endregion
    }
}